package com.sdry.service.impl.cb;

import com.sdry.mapper.cb.DepreciationMapper;
import com.sdry.model.cb.DepreciationEntity;
import com.sdry.service.cb.DepreciationService;
import com.sdry.utils.BigDecimalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DepreciationServiceImpl implements DepreciationService {

    @Autowired
    private DepreciationMapper depreciationMapper;

    @Override
    public List<DepreciationEntity> getList(String date,Long stockId) throws ParseException {
        List<DepreciationEntity> list = depreciationMapper.findPageWithResult(date,stockId);
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        getBigDeMonery(list,format1.parse(date+"-01"));
        return list;
    }

    public static BigDecimal getBigDeMonery(List<DepreciationEntity> list,Date date) {
        BigDecimal temp2 = new BigDecimal("0");
        for (DepreciationEntity de : list) {
            //折旧月份
            Integer totalMonth = getDifMonth(de.getStartDate(), de.getDueDate());
            de.setNumberMon(totalMonth);
            //累计折旧月份

            //当前时间已经完成的折旧判断
            Integer difMonth = getDifMonth(de.getStartDate(), date);
            if (date.getTime() > de.getDueDate().getTime()) {
                de.setAccuMon(totalMonth);

            }else {
                de.setAccuMon(difMonth);
            }

            //累计折旧金额
            String multiply = "0";
            if (totalMonth > 0) {
                multiply = BigDecimalUtil.multiply(BigDecimalUtil.divide(difMonth.toString(),totalMonth.toString()), de.getTotalMoney().toString());
            }
            BigDecimal bigDecimal = new BigDecimal(multiply);

            //当前时间已经完成的折旧判断
            if (date.getTime() > de.getDueDate().getTime()) {
                de.setAccuMoney(de.getTotalMoney());

            }else {
                de.setAccuMoney(bigDecimal);
            }

            //余额
            String balanceMoney = BigDecimalUtil.substract(de.getTotalMoney().toString(), multiply);
            BigDecimal bigDecimal1 = new BigDecimal(balanceMoney);
            de.setBalanceMoney(bigDecimal1);
            //当前折旧金额
            String temp1 = "0";
            if (bigDecimal.compareTo(new BigDecimal("0")) == 1) {
                temp1 = BigDecimalUtil.divide(de.getTotalMoney().toString(), totalMonth.toString(),2);

            }

            //当前时间已经完成的折旧判断，余额和本月折旧一起判断
            if (date.getTime() > de.getDueDate().getTime()) {
                de.setBalanceMoney(new BigDecimal(0));
                de.setParameter3(new BigDecimal(0));
                temp1 = "0";
            }

            BigDecimal decimal = new BigDecimal(temp1);
            de.setParameter3(decimal);
            temp2 = new BigDecimal(BigDecimalUtil.add(decimal.toString(),temp2.toString()));
        }
        BigDecimal t1 = new BigDecimal(0);
        for (int i = 0; i < list.size(); i++) {
            t1 = new BigDecimal(BigDecimalUtil.add(list.get(i).getParameter3().toString(),t1.toString()));
        }
        return t1;
    }

    @Override
    public Integer add(DepreciationEntity de) {
        de.setTotalMoney(new BigDecimal(de.getParameter2()));
        return depreciationMapper.insertDynamic(de);
    }

    @Override
    public Integer delete(String[] idArr) {
        Integer offerRow = 0;
        for (int i = 0; i < idArr.length; i++) {
            offerRow = depreciationMapper.setDelete(Long.parseLong(idArr[i]));
        }

        return offerRow;
    }


    /**
     * 两个时间相差月份
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return
     */
    public static Integer getDifMonth(Date startDate, Date endDate){
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(startDate);
        end.setTime(endDate);
        int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
        int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result + 1);
    }

}
